// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:49:08
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Load\FORCEParser.cpp
 */
#include "Import/BDFImport/BulkData/Load/FORCEParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IFORCEService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (FORCE);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/FORCE/FORCE.md>
*/
// 以下需要修改
enum FORCEkey : std::uint8_t
{
    FORCEKEY_SID = 2,  // Load set identification number. (Integer > 0)
    FORCEKEY_G   = 3,  // Grid point identification number. (Integer > 0)
    FORCEKEY_CID = 4,  // Coordinate system identification number. (Integer > 0; Default = 0)
    FORCEKEY_F   = 5,  // Scale factor. (Real)
    FORCEKEY_N1  = 6,  // Components of a vector measured in coordinate system defined by CID. (Real; at leastone Ni 0.0. unless F is zero)
    FORCEKEY_N2  = 7,
    FORCEKEY_N3  = 8,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseFORCE (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::FORCEData tempFORCE;

    // TODO(bug 001) m_dominId的正确填写，当前默认domainId设置为1
    tempFORCE.m_domainId = 1;

    // 解析条目信息，并分割成单个词条

    // 当前词条的索引，初始值为1
    int curIDKey = 1;

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前词条是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;  // 如果是空白，跳过当前词条
        }

        // 将词条转换为科学计数法格式

        // 根据当前词条索引，匹配对应的FORCE属性
        switch (curIDKey)
        {
        case FORCEKEY_SID:
            // 设置载荷集ID
            tempFORCE.m_sId = convertToI64 (curStrKey);
            break;
        case FORCEKEY_G:
            // 设置网格节点ID
            tempFORCE.m_nodeId = convertToI64 (curStrKey);
            break;
        case FORCEKEY_CID:
            // 设置坐标系ID
            tempFORCE.m_coordSysmId = convertToI64 (curStrKey);
            break;
        case FORCEKEY_F:
            // 设置比例因子
            tempFORCE.m_factor = convertToReal (curStrKey);
            break;
        case FORCEKEY_N1:
            // 设置载荷向量的N1分量
            tempFORCE.m_load[0] = convertToReal (curStrKey);
            break;
        case FORCEKEY_N2:
            // 设置载荷向量的N2分量
            tempFORCE.m_load[1] = convertToReal (curStrKey);
            break;
        case FORCEKEY_N3:
            // 设置载荷向量的N3分量
            tempFORCE.m_load[2] = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        // 处理完当前词条后，索引递增
        curIDKey++;
    }

    // 将解析好的FORCE数据添加到数据库服务
    return dbServiceFactory->get<IFORCEService> ()->Add (tempFORCE);
}